package com.quanshu.exchange.ui.gui.profile;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.subjects.BehaviorSubject;

/**
 * 购物车 - 加 - 减
 * Created by xiniu003 on 2018/1/16.
 */

public class DialogAddReduce extends BaseDialog {

    @BindView(R.id.add_shop)
    TextView addShop;
    @BindView(R.id.shop_nums)
    TextView shopNums;
    @BindView(R.id.reduce_shop)
    TextView reduceShop;
    @BindView(R.id.shop_enter)
    TextView shopEnter;

    private int currentNum;

    public DialogAddReduce(@NonNull Context context) {
        super(context);
    }

    //确定
    private BehaviorSubject<Integer> oneBehaviorSubject = BehaviorSubject.create();

    public BehaviorSubject<Integer> getOneBehaviorSubject() {
        return oneBehaviorSubject;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_reduce);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        //减少
        RxViewHelper.getInstance().clicks(reduceShop, aVoid -> {
            if (currentNum > 1) {
                currentNum -= 1;
            }
            setNums(currentNum);
        });

        //增加
        RxViewHelper.getInstance().clicks(addShop, aVoid -> {
            currentNum += 1;
            setNums(currentNum);
        });

        //确定
        RxViewHelper.getInstance().clicks(shopEnter, aVoid -> {
            oneBehaviorSubject.onNext(currentNum);
        });

    }

    public void setNums(int nums) {
        currentNum = nums;
        shopNums.setText(currentNum + "");
    }

    @Override
    public void init(@NonNull Context context) {
        Window window = getWindow();
        window.setWindowAnimations(R.style.BottomAnim);
        window.setGravity(Gravity.BOTTOM);
        //默认的Dialog只有5/6左右的宽度，改为全屏宽度,由dialog的布局自己来决定实际显示宽度
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    }

    @Override
    public boolean isCancelAble() {
        return true;
    }


}
